/*
 * Copyright (C) Michael Opitz
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA
 */

#include <boost/shared_ptr.hpp>

#include "handler/handler_factory.h"
#include "user_handler/web_handler.h"
#include "user_handler/web_acceptor.h"
#include "user_handler/music_handler.h"
#include "user_handler/music_acceptor.h"
#include "user_handler/ic_handler.h"
#include "user_handler/ic_acceptor.h"
#include "network/rs_socket.h"
#include "network/configurator.h"
#include "music/music_system.h"


int main(int argc, char *argv[])
{
	boost::shared_ptr<handler::handler_factory> f(
		new handler::handler_factory);
	f->build();

	{
	// creating sockets
	boost::shared_ptr<network::i_socket> music(
		f->get_connection_manager()->add_listener(12345));
	boost::shared_ptr<network::i_socket> web(
		f->get_connection_manager()->add_listener(12346));
	boost::shared_ptr<network::i_socket> ic(
		new network::rs_socket("/dev/ttyUSB0"));

	// we need an receiver for the rs-232 connection, because there is no
	// way of listening to an new connection over rs-232 :(
	f->get_connection_manager()->add_recv(ic, 
		network::configurator::get_rs232_filetype());

	boost::shared_ptr<music::music_system> music_sys(
		new music::music_system());
		
	// creating the acceptors and adding it to the multicaster
	boost::shared_ptr<user_handler::music_acceptor> music_acceptor(
		new user_handler::music_acceptor(f->get_connection_manager(), 
						 f->get_multicaster(),
						 music_sys));
	f->get_multicaster()->add_handler(
		network::CONNECT, music, music_acceptor);

	boost::shared_ptr<user_handler::web_acceptor> web_acceptor(
		new user_handler::web_acceptor(web, ic, 
			f->get_connection_manager(), 
			f->get_multicaster(),
			music_sys));
	f->get_multicaster()->add_handler(
		network::CONNECT, web, web_acceptor);
	boost::shared_ptr<user_handler::ic_acceptor> ic_acc(
		new user_handler::ic_acceptor(ic, web, 
			f->get_multicaster(), 
			f->get_connection_manager()));
	// yes, our ic_acceptor gets notified when a new web-connection is
	// established
	f->get_multicaster()->add_handler(
		network::CONNECT, web, ic_acc);

	}
	// our main-loop
	while(true) {
		usleep(10000000);
	}
	f.reset();

	return 0;
}
